<template>
    <view class="goods-item">
        <!-- 商品左侧 图片区域 -->
        <view class="goods-item-left">
            <!-- 复选框 -->
            <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClick"></radio>
            <!-- 图片 -->
            <image :src="goods.goods_small_logo|| defaultImg" class="goods-pic"></image>
        </view>
        <!-- 商品右侧 -->
        <view class="goods-item-right">
            <!-- 商品名称 -->
            <view class="goods-name">{{goods.goods_name}}</view>
            <!-- 商品信息 价格等等 -->
            <view class="goods-info-box">
                <!-- 商品价格 -->
                <view class="goods-price">￥{{goods.goods_price | toFixd}}</view>
                <!-- 步进器 -->
                <uni-number-box :min="1" :value="goods.goods_count" v-if="showRadio" @change="numChange">
                </uni-number-box>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        name: "my-goodsitem",
        props: {
            goods: {
                type: Object,
                required: true,
                default: {}
            },
            showRadio: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                // 默认图片
                defaultImg: 'http://n.sinaimg.cn/sinacn/w640h897/20180228/edb1-fyrzexa5887251.jpg'
            };
        },
        methods: {
            // 复选框的点击事件
            radioClick() {
                // 触发自定义事件 并把当前商品的id和最新选中状态传递给父组件
                this.$emit('radio-change', {
                    goods_id: this.goods.goods_id,
                    goods_state: !this.goods.goods_state
                })
            },
            // 步进器数值发生改变事件
            numChange(val) {
                // 把最新的数量值和该商品的id传递出去
                this.$emit('goodsCountChange', {
                    goods_id: this.goods.goods_id,
                    goods_count: val - 0
                })
            }
        },
        // 过滤器
        filters: {
            toFixd(price) {
                return Number(price).toFixed(2)
            }
        }
    }
</script>

<style lang="scss">
    .goods-item {
        width: 750rpx;
        box-sizing: border-box;
        display: flex;
        padding: 10px 5px;
        border-bottom: 1px solid #eee;

        .goods-item-left {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-right: 5px;

            .goods-pic {
                width: 100px;
                height: 100px;
                display: block;
            }
        }

        .goods-item-right {
            display: flex;
            flex: 1;
            flex-direction: column;
            justify-content: space-between;

            .goods-name {
                font-size: 13px;
            }

            .goods-info-box {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .goods-price {
                    color: #c00000;
                    font-size: 16px;
                }
            }
        }
    }
</style>
